import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Account implements Lockable{
	private int Id;
	private String Name;
	private int Money;
	public int ktr=1; //kiem tra tai khoan co bi lock hay khong
	
	public Account() throws IOException
	{
		System.out.println("Nhap id tai khoan");
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String id=br.readLine();
		this.Id=Integer.parseInt(id);
		BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Nhap ten tai khoan");
		this.Name=br2.readLine();
		System.out.println("Nhap so tien cua tai khoan");
		BufferedReader br3=new BufferedReader(new InputStreamReader(System.in));
		String mone=br3.readLine();
		this.Money=Integer.parseInt(mone);
		this.setKey(this.Id); // de Id cung chinh la key
	}
	public void hienthi()
	{
		System.out.println("Thong tin tai khoan: ");
		System.out.println("TK: " + String.valueOf(this.Id));
		System.out.println("Ten: " + this.Name);
		System.out.println("So tien:" + String.valueOf(this.Money));
		
	}
	public void naptien() throws IOException
	{
		System.out.println("Nhap so tien can nap");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String stien = br.readLine();	
		this.Money += Integer.parseInt(stien);
	}
	public void ruttien() throws IOException
	{
		System.out.println("Nhap so tien can rut");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String stien = br.readLine();
		int tien = Integer.parseInt(stien);
		if(this.Money < tien) {
			System.out.println("So tien hien tai khong du de rut");
		}
		else
			this.Money -= tien;
	}
	public static void chuyenkhoan(Account a,Account b) throws IOException //cai nay cung nhap tu ban phim
	{
		System.out.println("Nhap so tien can chuyen");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String stien = br.readLine();
		int tien=Integer.parseInt(stien);
		if( a.Money < tien) {
			System.out.println("So tien hien tai khong du de chuyen khoan");
		}
		else {
			a.Money -= tien;
			b.Money += tien;
			System.out.println("Chuyen tien thanh cong");
			a.hienthi();
			b.hienthi();// chuyen tien thanh cong hien thi thong bao
		}	
	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("Nhap tai khoan a");
		Account a=new Account();
		System.out.println("Nhap tai khoan b");
		Account b=new Account();
		Account c=new Account();
		c.lock(c.Id);// lock tai khoan c
		if(a.locked()==false && b.locked()==false)// khi hai tai khoan khong bi lock thi duoc quyen chuyen tien
			chuyenkhoan(a,b);
		else
			System.out.println("Khong du dieu kien chuyen tien tu a sang b");
		if(a.locked()==false && c.locked()==false)// vi c da bi lock nen khong chuyen tien duoc
			chuyenkhoan(a,c);
		else
			System.out.println("Khong du dieu kien chuyen tien tu a sang c");
		
	}

	@Override
	public void setKey(int skey) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("Ma chuyen khoan da duoc thiet lap la "+skey);
	}

	@Override
	public void lock(int key) {
		// TODO Auto-generated method stub
		ktr=0;
	}

	@Override
	public void unlock(int key) {
		// TODO Auto-generated method stub
		ktr=1;
	}

	@Override
	public boolean locked() {
		// TODO Auto-generated method stub
		if(ktr==1)
			return false;
		return true;		
	}

}